var app = getApp();

Page({
  data: {
    markers: [],
    tabs: [
      {
        id: 0,
        name: "我要停车",
        isActive: true
      },
      {
        id: 1,
        name: "我要取车",
        isActive: false
      },
    ],
    cars:[
      {
      id:0,
      name:"鄂A27",
      userid:"1",
      state:"已停车",
      garageID:"1"
      },
      {
      id:1,
      name:"鄂A234\\567",
      userid:"1",
      state:"空闲"
      },
      {
      id:2,
      name:"鄂A234567",
      userid:"1",
      state:"空闲"
      },
    ],
    history:[],
    value:'',

    carnum:'',
    checked:true,
    show:true,
    plateNumber:''
  },
  onLoad: function (e) {

    this.addMarkers()
    var value = e.value
    var {markers} = this.data
   
    for(let i=0;i<markers.length;i++){
      if(markers[i].name==value) {
        this.setData({
          'value.name': value,
          'value.distance':markers[i].distance,
          'value.address':markers[i].address,
          'value.id':markers[i].id
        })
      }
    }

    wx.request({
      url: 'https://www.zxapi.top/store/findAllGarages',
      success(res){
        console.log(res)
      }
    })
  },
  addMarkers() {
    this.setData({
      markers: app.globalData.sortMarkers
    })
  },
  
  //自定义事件 用来接收子组件传递的数据
  handleItemChange(e) {
    const { index } = e.detail;
    /**let tabs = this.data.tabs */
    let { tabs } = this.data;
    tabs.forEach((v, i) => i === index ? v.isActive = true : v.isActive = false);
    this.setData({ tabs })
  },

  park(e){
    console.log("停车");
    var that=this
    let cmd='PARK'
    let garageID=''
    if(this.data.value==''){
      garageID=this.data.markers[0].id
    }else{
      garageID=this.data.value.id;
    }
    let userID=wx.getStorageSync('openid',userID)
    let context=this.data.carnum+'请求在'+garageID+'号车库停车'
    console.log(e);
    if(this.data.carnum!=''){
    wx.request({
      url:'https://www.zxapi.top/test',
      data: {
        cmd,
        garageID,
        parkingRecordsID: '0',
        userID,
        bookLength: '0',
        context,
        sender: '127.0.0.1',
        receiver: 'zhixiang'
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res){
        console.log(res);
        if (res.data.cmd == 'PARKOK') {
          wx.showModal({
            title: '提示',
            content: that.data.carnum + '在' + res.data.garageID + '号车库' + '停车成功',
            //showCancel: false, 
            success(res) {
              console.log(res);
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '停车失败',
            // showCancel: false
          })
        }
      }
    })
    }else {
      wx.showModal({
        title: '提示',
        content: '请输入车牌号！', 
      })
    }
  },
  fetch(e){
    console.log("取车");
    var that=this
    let cmd='FETCH'
    let garageID=''
    if(this.data.value==''){
      garageID=this.data.markers[0].id
    }else{
      garageID=this.data.value.id;
    }
    let userID=wx.getStorageSync('openid',userID)
    let context=this.data.carnum+'请求在'+garageID+'号车库取车'
    if(this.data.carnum!=''){
    wx.request({
      url:'https://www.zxapi.top/test',
      data: {
        cmd,
        garageID,
        parkingRecordsID: '0',
        userID,
        bookLength: '0',
        context,
        sender: '127.0.0.1',
        receiver: 'zhixiang'
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res){
        if (res.data.cmd == 'FETCHOK') {
          // that.generateOrder()
          wx.showModal({
            title: '提示',
            content: that.data.carnum + '在' + res.data.garageID + '号车库' + '取车成功',
            //showCancel: false, 
            success(res) {
              console.log(res);
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '取车失败', 
            // showCancel: false
          })
        }
      }
    })
    }else {
      wx.showModal({
        title: '提示',
        content: '请输入车牌号！', 
      })
    }
  },
  //生成商户订单
  generateOrder: function () { 
    var that=this
    var openid=wx.getStorageSync('openid',openid)
    //统一支付返回package
    wx.request({
      url:'https://www.zxapi.top/wxpay/getpayid',
      method:'POST',
      data:{
        openid:openid,
        ip:"221.232.59.167",
        body:"停车支付",
        total_fee:1
      },
      success:function(res){
        console.log(res);
        var pay=res.data.data
        var timeStamp = pay.timeStamp;
        var packages = pay.package;
        var paySign = pay.paySign;
        var nonceStr = pay.nonceStr;
        var params = { "timeStamp": timeStamp,"nonceStr": nonceStr, "package": packages, "signType": "MD5","paySign": paySign};
        that.pay(params)
      }
    })
  },
  //支付
  pay:function(params){
    console.log(params)
    console.log("支付")
    // console.log(params)
    wx.requestPayment({
      timeStamp: params.timeStamp,
      nonceStr: params.nonceStr,
      package: params.package,
      signType:params.signType,
      paySign: params.paySign,
      success:function(res){
        console.log(res);
      },
      fail:function(res){
        console.log(res)
      }
    })
  },

  // 清空page对象data的history数组 重置缓存为[]
  clearHistory: function() {
    this.setData({
      history: []
    })
    wx.setStorageSync("history", [])
  },
  //历史记录的点击事件
  chooseNum:function(e){
    console.log("111");
    console.log(this.data.plateNumber);
    this.setData({
      plateNumber:e.currentTarget.id.split(''),
      checked:false
    })
    console.log(this.data.plateNumber);
    console.log(e);
    console.log("111");
  },
  //每次显示钩子函数都去读一次本地storage
  onShow: function() {
    this.setData({
      history: wx.getStorageSync("history") || []
    })
  },

  onChange(e){
    this.setData({
      plateNumber:e.detail
    })
    console.log(this.data.plateNumber);
  },
  onOk(e){
    console.log("11");
    this.setData({
      carnum:e.detail,
      checked:false,
    })
    var history = wx.getStorageSync("history")
    console.log(this.data.carnum);
    console.log(typeof(this.data.history));
    history.push(e.detail);
    wx.setStorageSync("history", history)
    console.log("111");
 }
})